<?php

namespace App\Filament\Pages\Permission;

use Filament\Actions\Action;
use Filament\Actions\Concerns\InteractsWithActions;
use Filament\Actions\Contracts\HasActions;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Notifications\Notification;
use Filament\Pages\Page;
use Filament\Schemas\Components\Tabs;
use Filament\Schemas\Components\Tabs\Tab;
use Illuminate\Support\Facades\Hash;

class Profile extends Page implements HasActions, HasForms
{
    use InteractsWithActions;
    use InteractsWithForms;

    public array $data = [];

    public string $view = 'filament.pages.profile';

    public static function shouldRegisterNavigation(): bool
    {
        return false;
    }

    public function getTitle(): string
    {
        return '';
    }

    public function getFormStatePath(): string
    {
        return 'data';
    }

    public function getFormSchema()
    {
        return Tabs::make('Tabs')
            ->tabs([
                Tab::make('password')
                    ->label('密码设置')
                    ->schema([
                        TextInput::make('old_password')
                            ->label('原登录密码')
                            ->placeholder('请输入原登录密码')
                            ->password()
                            ->required()
                            ->revealable()
                            ->maxLength(16),
                        TextInput::make('password')
                            ->label('新登录密码')
                            ->placeholder('请输入新登录密码')
                            ->password()
                            ->required()
                            ->revealable()
                            ->minLength(6)
                            ->maxLength(16),
                        TextInput::make('confirm_password')
                            ->label('确认密码')
                            ->placeholder('请再输入一次新登录密码')
                            ->password()
                            ->same('password')
                            ->required()
                            ->revealable()
                            ->minLength(6)
                            ->maxLength(16),
                        Action::make('submit')
                            ->label('提交保存')
                            ->action(function (array $data) {
                                $data = $this->form->getState();
                                if (! Hash::check($data['old_password'], auth()->user()->password)) {
                                    Notification::make('error')->title('原密码错误')->danger()->send();

                                    return;
                                }
                                auth()->user()->update([
                                    'password' => $data['password'],
                                ]);
                                Notification::make('success')->title('修改成功')->success()->send();

                                auth()->logout();
                            }),
                    ]),
            ]);
    }
}
